package playing;
import object.*;
import java.sql.*;
import java.util.Date;

public class PlantDataReader {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/ZUT";
    private static final String USER = "root";
    private static final String PASS = "123456";

    //查询植物信息
    public Plant getPlantById(int plantId) {
        String sql = "SELECT plant_id, plant_name, growth_level, last_interaction_time FROM plants WHERE plant_id = ?";
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, plantId);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    int id = rs.getInt("plant_id");
                    String name = rs.getString("plant_name");
                    int growthLevel = rs.getInt("growth_level");
                    Timestamp lastInteractionTime = rs.getTimestamp("last_interaction_time");
                    return new Plant(id, name, growthLevel, lastInteractionTime);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 如果没有找到对应的植物，返回null
        return null;
    }
    //检查植物名称是否存在
    public boolean isPlantNameExists(String plantName) {
        String sql = "SELECT COUNT(*) FROM plants WHERE plant_name = ?";
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, plantName);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    // 如果COUNT(*)大于0，则存在该名称的植物
                    return rs.getInt(1) > 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 如果发生异常或查询未返回结果，则默认返回false
        return false;
    }
    //按名字查询植物id
    public Integer getPlantIdByName(String plantName) {
        String sql = "SELECT plant_id FROM plants WHERE plant_name = ?";
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, plantName);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    // 假设植物名称是唯一的，或者我们只关心第一个匹配的ID
                    return rs.getInt("plant_id");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 如果没有找到匹配的植物，返回null或者一个特殊的ID（比如-1）来表示没有找到
        return null; // 或者根据你的需要返回其他值
    }
    // 将植物数据写入数据库
    public boolean savePlant(Plant plant) {
        String sql;
        if (plant.getPlantId() > 0) { // 更新
            sql = "UPDATE plants SET plant_name = ?, growth_level = ?, last_interaction_time = ? WHERE plant_id = ?";
        } else { // 插入
            sql = "INSERT INTO plants (plant_name, growth_level, last_interaction_time) VALUES (?, ?, ?)";
        }
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            if (plant.getPlantId() > 0) {
                pstmt.setString(1, plant.getPlantName());
                pstmt.setInt(2, plant.getGrowthLevel());
                pstmt.setTimestamp(3, new Timestamp(plant.getLastInteractionTime().getTime()));
                pstmt.setInt(4, plant.getPlantId());
            } else {
                pstmt.setString(1, plant.getPlantName());
                pstmt.setInt(2, plant.getGrowthLevel());
                pstmt.setTimestamp(3, new Timestamp(plant.getLastInteractionTime().getTime()));
            }

            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false; // 如果发生错误，则返回false
    }
    //删除
    public boolean deletePlantById(int plantId) {
        String sql = "DELETE FROM plants WHERE plant_id = ?";
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, plantId);
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false; // 如果发生错误，则返回false
    }

}